﻿using System;
using System.Xml.Linq;
using ContactTracing.Core;

namespace ContactTracing.ViewModel
{
    public class Contact
    {
        private FollowUpWindowViewModel _followUpWindowViewModel;
        private double? _age;
        private double? _ageYears;
        public int _uniqueKey = 0;
        public string _contactID = String.Empty;
        public bool IsLocked { get; set; }

        public string RecordId { get; set; }
        public string Surname { get; set; }
        public string OtherNames { get; set; }
        public string Gender { get; set; }
        public DateTime? FirstSaveTime { get; set; }
        public AgeUnits? AgeUnit { get; set; }
        public DateTime? DateOfLastContact { get; set; }
        public CaseViewModel LastSourceCase { get; set; }
        public string RiskLevel { get; set; }
        public string HeadOfHousehold { get; set; }
        public string Village { get; set; }
        public string SubCounty { get; set; }
        public string District { get; set; }
        public string LC1Chairman { get; set; }
        public string Phone { get; set; }
        public string HCW { get; set; }
        public string HCWFacility { get; set; }
        public string FinalOutcome { get; set; }
        public bool IsCase { get; set; }
        public bool IsActive { get; set; }
        public string ContactID
        {
            get
            {
                return this._contactID;
            }
            private set
            {
                this._contactID = value;
            }
        }

        public int UniqueKey
        {
            get { return this._uniqueKey; }
            set
            {
                this._uniqueKey = value;
                this.ContactID = "C-" + value.ToString("D4");
            }
        }

        public string IsCaseSymbol
        {
            get
            {
                if (IsCase)
                {
                    return "‼";
                }
                else
                {
                    return String.Empty;
                }
            }
        }

        public FollowUpWindowViewModel FollowUpWindowViewModel 
        {
            get
            {
                return this._followUpWindowViewModel;
            }
            set
            {
                this._followUpWindowViewModel = value;
            }
        }

        public double? Age
        {
            get { return this._age; }
            set
            {
                this._age = value;

                if (AgeUnit == AgeUnits.Years)
                {
                    AgeYears = Math.Round(this.Age.Value, 2);
                }
                else if (AgeUnit == AgeUnits.Months && Age.HasValue)
                {
                    double newAge = (Age.Value / 12);
                    AgeYears = Math.Round(newAge, 2);
                }
                //else if (AgeUnit == null) { throw new ApplicationException("Age unit cannot be null in Age setter."); } // form updated to force AgeUnit if Age has a value, so this is no longer needed
                else
                {
                    AgeYears = null;
                }
            }
        }

        public double? AgeYears
        {
            get
            {
                return _ageYears;
            }
            private set
            {
                this._ageYears = value;
            }
        }

        public Contact()
        {
            IsLocked = false;
            Surname = String.Empty;
            OtherNames = String.Empty;
            Gender = String.Empty;
            RiskLevel = String.Empty;
            HeadOfHousehold = String.Empty;
            Village = String.Empty;
            SubCounty = String.Empty;
            District = String.Empty;
            LC1Chairman = String.Empty;
            Phone = String.Empty;
            HCW = String.Empty;
            HCWFacility = String.Empty;
            FinalOutcome = String.Empty;
            IsActive = true;
            IsCase = false;
        }

        public XElement Serialize()
        {
            XElement recordElement =
                new XElement("Record",
                    new XAttribute("GlobalRecordId", this.RecordId.ToString()));

            if(this.FirstSaveTime.HasValue) 
            {
                recordElement.Add(new XAttribute("FirstSaveTime", this.FirstSaveTime.Value.Ticks.ToString()));
            }
            else 
            {
                recordElement.Add(new XAttribute("FirstSaveTime", String.Empty));
            }

            string age = String.Empty;
            if (Age.HasValue)
            {
                age = Age.Value.ToString();
            }

            object ageUnit = String.Empty;
            if (AgeUnit.HasValue)
            {
                ageUnit = (int)AgeUnit.Value;
            }

            recordElement.Add(
                new XElement("Surname", this.Surname),
                new XElement("OtherNames", this.OtherNames),
                new XElement("Age", age),
                new XElement("AgeUnit", ageUnit),
                new XElement("Gender", this.Gender),
                new XElement("RiskLevel", this.RiskLevel),
                new XElement("HeadOfHousehold", this.HeadOfHousehold),
                new XElement("Village", this.Village),
                new XElement("SubCounty", this.SubCounty),
                new XElement("District", this.District),
                new XElement("LC1Chairman", this.LC1Chairman),
                new XElement("Phone", this.Phone),
                new XElement("HCW", this.HCW),
                new XElement("HCWFacility", this.HCWFacility),
                new XElement("FinalOutcome", this.FinalOutcome)
                );

            return recordElement;
        }
    }
}
